<template>
  <el-dialog v-model="visible" :title="!dataForm.predictCmdId ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="dataForm.deviceType" placeholder="请选择设备类型" size="large">
          <el-option v-for="item in deviceTypeList" :key="item.id" :label="item.deviceDesc" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="指令类型" prop="cmdCode">
        <el-select v-model="dataForm.cmdCode" placeholder="请选择指令类型" size="large" @change="selectDeviceType">
          <el-option v-for="(item, index) in cmdTypeList" :key="index" :value="item" :label="item.cmdmId + '-' + item.cmdName + '(' + item.cmdCode + ')'" />
        </el-select>
      </el-form-item>
      <el-form-item label="指令名称" prop="cmdName">
        <el-input v-model="dataForm.cmdName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="是否同步" prop="sync">
        <el-switch v-model="dataForm.sync" />
      </el-form-item>
      <el-form-item label="指令描述" prop="cmdDescr">
        <el-input v-model="dataForm.cmdDescr" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="指令密码" prop="cmdPwd">
        <el-input v-model="dataForm.cmdPwd" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="参数类型" prop="cmdType">
        <el-select v-model="dataForm.cmdType" placeholder="" size="large">
          <el-option v-for="(v, k) in { text: '文本输入', dropDown: '下拉选择' }" :key="k" :label="v" :value="k" />
        </el-select>
      </el-form-item>
      <el-form-item label="参数" prop="params">
        <el-input v-model="dataForm.params" :autosize="{ minRows: 2, maxRows: 4 }" type="textarea" placeholder="Please input" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useCommandTypeApi, useCommandTypeSubmitApi, getCommandTypeApi } from '@/api/gps/command_type'
import { getProductByUser } from '@/api/gps/product'

import { inject } from 'vue'

const { getDataList, getSourceList } = inject<any>('location')

const emit = defineEmits(['refreshDataList'])
const deviceTypeList = ref()
const cmdTypeList = ref()
const visible = ref(false)
const dataFormRef = ref()
const dataForm = reactive({
  predictCmdId: undefined,
  deviceType: '',
  cmdCode: '',
  cmdName: '',
  // cmdNameEn: '',
  // cmdNameTw: '',
  cmdDescr: '',
  // cmdDescrEn: '',
  // cmdDescrTw: '',
  params: '',
  // paramsEn: '',
  // paramsTw: '',
  cmdPwd: undefined,
  // cmdLevel: '',
  sync: 1,
  cmdType: 'text'
})

const selectDeviceType = (res: any) => {
  console.log(res)
  dataForm.cmdCode = res.cmdCode
  dataForm.cmdName = res.cmdName
  dataForm.params = res.params
  dataForm.cmdDescr = res.cmdDescr
}
//获取所有类型
function EquipmentList() {
  getProductByUser().then((res) => {
    deviceTypeList.value = res.data
  })
  getCommandTypeApi().then((res) => {
    cmdTypeList.value = res.data
  })
}
const init = (id?: number) => {
  EquipmentList()
  visible.value = true
  dataForm.predictCmdId = undefined

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
  }

  if (id) {
    getCommandType(id)
  }
}

const getCommandType = (id: number) => {
  useCommandTypeApi(id).then((res) => {
    Object.assign(dataForm, res.data)
  })
}

const dataRules = ref({
  cmdCode: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  deviceType: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  sync: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }

    useCommandTypeSubmitApi(dataForm).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          getDataList()
          getSourceList()
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>
